package com.yuanli.cosmeticmirror.view.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cgfay.filter.glfilter.resource.bean.ResourceData;
import com.comenjoysoft.baselibrary.util.ThreadUtils;
import com.kongzue.dialog.v3.CustomDialog;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseFragment;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.bean.PhotoFrame;
import com.yuanli.cosmeticmirror.bean.PhotoFrameBean;
import com.yuanli.cosmeticmirror.event.MainEvent;
import com.yuanli.cosmeticmirror.model.iview.IPhotoFrameView;
import com.yuanli.cosmeticmirror.persenter.PhotoFramePersenter;
import com.yuanli.cosmeticmirror.view.activity.FilterActivity;
import com.yuanli.cosmeticmirror.view.activity.MemberActivity;
import com.yuanli.cosmeticmirror.view.activity.PhotoFrameActivity;
import com.yuanli.cosmeticmirror.view.adapter.PhotoFrameAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;
import rxhttp.wrapper.param.RxHttp;

import static com.yuanli.cosmeticmirror.base.Constant.ACTION_START_PHOTO_FRAME_SWITCHING;


public class TabFragment extends BaseFragment implements IPhotoFrameView {
    @BindView(R.id.tab_rv)
    RecyclerView tabRv;
    private int id;
    private int index;
    private PhotoFrameAdapter adapter;
    private PhotoFramePersenter persenter;
    private PhotoFrameActivity activity;

    public TabFragment(int index, int id) {
        this.index = index;
        this.id = id;
        persenter = new PhotoFramePersenter(this, this);
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_tab;
    }

    @Override
    protected void init(Bundle paramBundle) {

    }

    @Override
    protected void initView(View view) {
        activity = (PhotoFrameActivity) getActivity();
        tabRv.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter = new PhotoFrameAdapter();
        tabRv.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view1, position) -> {
            PhotoFrameBean bean = (PhotoFrameBean) adapter.getItem(position);
            if (bean != null) {
                if (!TextUtils.equals(bean.getIsvip(), "1") && !MyApp.IsVIP) {
                    getCustomDialogPaly();
                    return;
                }
                bean.setCheck(MyApp.getPhotoFrameStatus().checkPosition == position);
                if (!bean.isCheck()) {
                    bean.setCheck(true);
                    MyApp.getPhotoFrameStatus().fragmentIndex = index;
                    MyApp.getPhotoFrameStatus().checkPosition = position;
                    showLoadDialog();
                    RxHttp.get(bean.getBigimgUrl()).asBitmap().as(RxLife.asOnMain(TabFragment.this)).subscribe(new Consumer<Bitmap>() {
                        @Override
                        public void accept(Bitmap bitmap) throws Exception {
                            dialogDismiss();
                            EventBus.getDefault().post(new MainEvent<>(ACTION_START_PHOTO_FRAME_SWITCHING, bitmap));
                        }
                    }, throwable -> {
                        dialogDismiss();
                        showToast("相框资源获取失败");
                    });
                } else {
                    MyApp.getPhotoFrameStatus().fragmentIndex = -1;
                    MyApp.getPhotoFrameStatus().checkPosition = -1;
                    EventBus.getDefault().post(new MainEvent<>(ACTION_START_PHOTO_FRAME_SWITCHING, null));
                    bean.setCheck(false);
                }
                this.adapter.setPosition(position);
            }
            Logger.d("fragmentIndex" + MyApp.getPhotoFrameStatus().fragmentIndex + "position  " + MyApp.getPhotoFrameStatus().checkPosition);
        });
    }

    @Override
    protected void startLoadData() {
        persenter.getPhotoFrameById(String.valueOf(id));
        Log.d("startLoadData", "startLoadData: " + id);

    }

    @Override
    protected void stopLoadData() {
        Log.d("startLoadData", "stopLoadData: " + id);
    }

    @Override
    public void sucess(PhotoFrame object) {

    }

    @Override
    public void error(String msg) {
        showToast(msg);
    }

    @Override
    public void getPhotoFrame(List<PhotoFrameBean> object) {
        adapter.setNewInstance(object);
        Logger.d("getPhotoFrame fragmentIndex" + MyApp.getPhotoFrameStatus().fragmentIndex + "position  " + MyApp.getPhotoFrameStatus().checkPosition);
        if (MyApp.getPhotoFrameStatus().fragmentIndex == index && MyApp.getPhotoFrameStatus().checkPosition != -1) {
            adapter.setPosition(MyApp.getPhotoFrameStatus().checkPosition);
            adapter.notifyItemRangeChanged(0, adapter.getItemCount());
        }

    }

    /**
     * 滤镜界面  提示需要会员
     *
     * @return
     */
    private CustomDialog getCustomDialogPaly() {
        return CustomDialog.show((AppCompatActivity) requireActivity(), R.layout.dialog_member_warm_2, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.member_enter);
            TextView btncancel = v.findViewById(R.id.member_cancel);
            btnOk.setOnClickListener(v1 -> {
                startActivity(new Intent(requireActivity(), MemberActivity.class));
                requireActivity().finish();
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> {
                dialog.doDismiss();
            });
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }


}
